package com.qianfeng.missfood.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.qianfeng.missfood.BaseApp;
import com.qianfeng.missfood.R;
import com.qianfeng.missfood.adapter.GuidePagerAdapter;
import com.qianfeng.missfood.constants.AppConfig;
import com.qianfeng.missfood.constants.ResourceConstants;

import java.util.ArrayList;

public class GuideActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener, View.OnTouchListener {

    private ImageView[] guideDots;
    private int pager;
    private int lastX = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();

    }

    private void initView() {

        ArrayList<ImageView> imageViews = new ArrayList<>();

        for (int i = 0; i < ResourceConstants.guideRes.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(ResourceConstants.guideRes[i]);
            imageViews.add(imageView);
        }
        GuidePagerAdapter guidePagerAdapter = new GuidePagerAdapter(imageViews, this);
        ViewPager guideViewPager = (ViewPager) findViewById(R.id.guideViewPager);
        guideViewPager.setOnTouchListener(this);
        guideViewPager.setAdapter(guidePagerAdapter);
        guideViewPager.addOnPageChangeListener(this);


        ImageView guideDot1 = (ImageView) findViewById(R.id.guideDot1);
        ImageView guideDot2 = (ImageView) findViewById(R.id.guideDot2);
        ImageView guideDot3 = (ImageView) findViewById(R.id.guideDot3);
        ImageView guideDot4 = (ImageView) findViewById(R.id.guideDot4);

        guideDots = new ImageView[]{guideDot1, guideDot2, guideDot3, guideDot4};

        Button guideRegister = (Button) findViewById(R.id.guideRegister);
        guideRegister.setOnClickListener(this);
        Button guideLogin = (Button) findViewById(R.id.guideLogin);
        guideLogin.setOnClickListener(this);
        Button guideEnter = (Button) findViewById(R.id.guideEnter);
        guideEnter.setOnClickListener(this);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        pager = position;
    }

    @Override
    public void onPageSelected(int position) {

        for (int i = 0; i < guideDots.length; i++) {

            if (position == i) {

                guideDots[i].setImageResource(R.mipmap.guide_dot_green);
            } else {

                guideDots[i].setImageResource(R.mipmap.guide_dot_white);
            }

        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {


    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.guideEnter:
                startAction(MainActivity.class);
                break;
            case R.id.guideRegister:

                startActionForResult(RegisterActivity.class);
                break;
            case R.id.guideLogin:

                startActionForResult(LoginActivity.class);
                break;
            default:
                break;
        }

    }

    private void startAction(Class<?> cls) {

        Intent intent = new Intent(this, cls);
        startActivity(intent);
        this.finish();
    }

    private void startActionForResult(Class<?> cls) {

        Intent intent = new Intent(this, cls);
        startActivityForResult(intent, AppConfig.GUIDE_REQUEST);

    }

    @Override
    public void onBackPressed() {

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                lastX = (int) event.getX();
                break;

            case MotionEvent.ACTION_MOVE:

                if (lastX - event.getX() > 100 && (pager == guideDots.length - 1)) {

                    startAction(MainActivity.class);

                }
                break;
            default:
                break;

        }

        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (BaseApp.getInstance().getLoginState()) {

            startAction(MainActivity.class);
        }

    }
}
